package com.liberation.aidiancan.base;

/**
 * 项目名称： AiDianCan
 * 创建人 : Liberation
 * 创建时间: 2016/12/12 9:14
 */


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.liberation.aidiancan.presenter.BasePresent;
import com.liberation.aidiancan.view.MvpView;


public abstract class BaseFragment<V extends MvpView,T extends BasePresent<V>> extends Fragment {
	public T presenter;
	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		presenter=createPresenter();
		Log.e("msg", "onCreate: 创建连接");
		if (presenter!=null) {
			this.presenter.attch((V) this);
		}
	}
	public abstract T createPresenter();
	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


		if (getContentView()==0) {
			return null;
		}
		View view = inflater.inflate(getContentView(), container, false);

		return view;
	}

	/**
	 * 该方法会紧随onCreateView被调用后调用
	 * @param view
	 * @param savedInstanceState
	 */
	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		init(view);
		loadDatas();
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getBundle(getArguments());
	}

	//获得bundle数据
	protected void getBundle(Bundle bundle) {

	}


	/**
	 * 设置fragment需要展示的布局ID
	 * @return
	 */
	protected abstract int getContentView();

	/**
	 * 初始化
	 * @param view
	 */
	protected abstract void init(View view);

	/**
	 * 加载数据
	 */
	protected abstract void loadDatas();

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (presenter!=null) {
			presenter.DeAttch((V) this);
		}
	}
}
